<?

define("CFG_LANG","fr_FR");

function armory_getGuild($guild=CFG_GUILDE){
	$url = "http://".CFG_SERVEUR.".battle.net/api/wow/guild/".CFG_ROYAUME."/".$guild."?fields=members,achievements,news&locale=".CFG_LANG;
	for($i=0;$i<5; $i++){
		$json=armory_getDataJSON($url);		
		if($json!=false) return $json;
		sleep(rand(1,2));
	}
	return false;
}

function armory_getPlayer($name){
	$url = "http://".CFG_SERVEUR.".battle.net/api/wow/character/".CFG_ROYAUME."/".$name."?fields=guild,professions,talents,items&locale=".CFG_LANG;
	for($i=0;$i<5; $i++){
		$json=armory_getDataJSON($url) ;
		if($json!=false) return $json;
		sleep(rand(1,2));
	}
	return false;
}

function armory_getRealm($realm=CFG_ROYAUME){

	$url = "http://".CFG_SERVEUR.".battle.net/api/wow/realm/status?realms=".$realm."&locale=".CFG_LANG;
	for($i=0;$i<5; $i++){
		$json=armory_getDataJSON($url) ;
		if($json!=false) return $json;
		sleep(rand(1,2));
	}
	return false;
}

function armory_getDataJSON($url) {
	$f = "";
	if(function_exists('curl_init')) {
		$ch = curl_init();
		$timeout = 5; // set to zero for no timeout
		$useragent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.5 Safari/537.17";
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
		$str  = array(
			"Accept-Language: fr",
			"Accept-Charset: ISO-8859-1,utf-8;",
		);
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $str );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 0 );
		curl_setopt ( $ch, CURLOPT_FORBID_REUSE, 1 );
		curl_setopt ( $ch, CURLOPT_LOW_SPEED_LIMIT, 5 );
		curl_setopt ( $ch, CURLOPT_LOW_SPEED_TIME, $timeout );
		curl_setopt ( $ch, CURLOPT_TIMEVALUE, $timeout*3 );
		$f = curl_exec($ch);
		curl_close($ch);			
	} else {
		$f = file_get_contents($url);
	}
	$json = json_decode($f);
	return $json;
}

/**
DEPRECTATED SINCE WOW API GIVES JSON 
**/
/*
function armory_getDataXML($url) {
	$f = "";
	if(function_exists('curl_init')) {
		$ch = curl_init();
		$timeout = 5; // set to zero for no timeout
		$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11";
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
		$str  = array(
			"Accept-Language: fr",
			"Accept-Charset: ISO-8859-1,utf-8;",
		);
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, $str );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 0 );
		curl_setopt ( $ch, CURLOPT_FORBID_REUSE, 1 );
		curl_setopt ( $ch, CURLOPT_LOW_SPEED_LIMIT, 5 );
		curl_setopt ( $ch, CURLOPT_LOW_SPEED_TIME, $timeout );
		curl_setopt ( $ch, CURLOPT_TIMEVALUE, $timeout*3 );
		$f = curl_exec($ch);
		curl_close($ch);			
	} else {
		$f = file_get_contents($url);
	}
	$xml = simplexml_load_string($f);
	return $xml;
}
*/
